﻿
Partial Class UCMain_Track_application
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsNothing(Session("section")) Then
            Session.Clear()
            Response.Redirect("../Login.aspx")
        End If
        If Not IsPostBack Then
            img.Visible = False
            lblerror.Text = ""
            loadDataLogin()
            populatedata()
        End If
    End Sub

    Sub clearSc()
        TxtPicopNo.Text = ""
        txtNamePICOP.Text = ""
        TxtCompPicop.Text = ""
        TxtTelPicop.Text = ""
        TxtWorkApp.Text = ""
        TxtComWorkapp.Text = ""
        TxtTelWorkapp.Text = ""
        txtDateStart.Text = ""
        txtDateEnd.Text = ""
        chkWest.Checked = False
        chkEast.Checked = False
        rdoWorkLocate.SelectedValue = Nothing
        DDlWorkLocate_From.ClearSelection()
        DDlWorkLocate_To.ClearSelection()
        DDlWorkLocate_FromS.ClearSelection()
        DDlWorkLocate_ToS.ClearSelection()
        rdoEnergize.SelectedValue = Nothing
        CheckBoxE_1_6.ClearSelection()
        CheckBoxW_1_6.ClearSelection()
        rdoTrain_Vihicle.ClearSelection()
        rdoTrain_Vihicle2.ClearSelection()
        TxtDes_work.Text = ""

    End Sub

    Sub clearChk()
        CheckBoxW_1_6.ClearSelection()
        CheckBoxE_1_6.ClearSelection()
        chkT.ClearSelection()
        chkV.ClearSelection()
    End Sub

    Sub genTPNO()
        Dim sql As String = ""
        'TP2012W33-02
        Dim tpFormat As String = "TP{0}W{1}-{2}"
        Dim y As String = CStr(Now.Date.Year)
        Dim w As String = CStr(Math.Round(Now.Date.DayOfYear / 7).ToString("00"))
        Dim r As Integer = 0
        Dim rC As String = ""
        Dim tpno As String = ""
        sql = " select max(right(TP_NO,2))+1 as cMax from [request_table] "
        Dim dt As New Data.DataTable
        dt = clsCommand.SQL_exec(sql)
        If dt.Rows.Count > 0 Then
            If dt.Rows(0)("cMax").ToString = "" Then
                r = 1
            Else
                r = dt.Rows(0)("cMax")
            End If
            rC = r.ToString("00")
        End If
        tpno = String.Format(tpFormat, y, w, rC)
        lblTPNO.Text = tpno
    End Sub
    Sub populatedata()
        If Not IsNothing(Request.QueryString("case")) Then
            If Request.QueryString("case") = "view" Then ' Approved
                Panel1.Enabled = False
                lblTPNO.Visible = True
                lblTPNOS.Visible = True
                genTPNO()
                btnAP.Visible = True
                btnCancel.Visible = True
            Else
                btnAP.Visible = False
                btnCancel.Visible = False
                lblTPNO.Visible = False
                Panel1.Enabled = True
            End If
        End If
        If Not IsNothing(Request.QueryString("ID_REQUEST_FORM")) Then
            lblidREQ.Text = Request.QueryString("ID_REQUEST_FORM")

            Dim sql As String = ""
            sql = " select * from request_table where ID_REQUEST_FORM = " & Request.QueryString("ID_REQUEST_FORM")
            Dim dt As New Data.DataTable

            dt = clsCommand.SQL_exec(sql)
            If dt.Rows.Count > 0 Then
                lblIDPICop.Text = dt.Rows(0)("WORK_ID_PICOP")
                Dim sqls As String = " select id_picop from user_table where ID_USER =" & lblIDPICop.Text & ""
                Dim dt2 As New Data.DataTable
                dt2 = clsCommand.SQL_exec(sqls)
                If dt2.Rows.Count > 0 Then
                    TxtPicopNo.Text = dt2.Rows(0)("id_picop")
                End If
                search()
                TxtWorkApp.Text = dt.Rows(0)("WORK_APPROVED_BY")
                TxtComWorkapp.Text = dt.Rows(0)("COMPANY_SECTION")
                TxtTelWorkapp.Text = dt.Rows(0)("MOBILE_PHONE")
                txtDateStart.Text = dt.Rows(0)("DUR_WORK_FORM")
                txtDateEnd.Text = dt.Rows(0)("DUR_WORK_TO")
                If dt.Rows(0)("WORK_LOCA_W").ToString = "1" Then
                    chkWest.Checked = True
                Else
                    chkWest.Checked = False
                End If
                If dt.Rows(0)("WORK_LOCA_E").ToString = "1" Then
                    chkEast.Checked = True
                Else
                    chkEast.Checked = False
                End If
                If dt.Rows(0)("WORK_LOCA_SPECIFY").ToString = "1" Then
                    rdoWorkLocate.SelectedValue = "1"
                Else
                    rdoWorkLocate.SelectedValue = "2"
                End If
                checkWorkLocation()
                DDlWorkLocate_From.SelectedValue = dt.Rows(0)("WORK_LOCA_FROM")
                DDlWorkLocate_To.SelectedValue = dt.Rows(0)("WORK_LOCA_TO")
                DDlWorkLocate_FromS.SelectedValue = dt.Rows(0)("WORK_LOCA_FROM_S")
                DDlWorkLocate_ToS.SelectedValue = dt.Rows(0)("WORK_LOCA_TO_S")
                txtDescript_area_from.Text = dt.Rows(0)("WORK_DESC_FROM")
                txtDescript_area_to.Text = dt.Rows(0)("WORK_DESC_TO")
                rdoEnergize.SelectedValue = dt.Rows(0)("POWER_REQ")
                If rdoEnergize.SelectedValue = "0" Then
                    CheckBoxE_1_6.Visible = True
                    CheckBoxW_1_6.Visible = True
                End If
                clearChk()
                If dt.Rows(0)("E1").ToString = "E1" Then
                    CheckBoxE_1_6.Items(0).Selected = True
                End If
                If dt.Rows(0)("E2").ToString = "E2" Then
                    CheckBoxE_1_6.Items(1).Selected = True
                End If
                If dt.Rows(0)("E3").ToString = "E3" Then
                    CheckBoxE_1_6.Items(2).Selected = True
                End If
                If dt.Rows(0)("E4").ToString = "E4" Then
                    CheckBoxE_1_6.Items(3).Selected = True
                End If
                If dt.Rows(0)("E5").ToString = "E5" Then
                    CheckBoxE_1_6.Items(4).Selected = True
                End If
                If dt.Rows(0)("E6").ToString = "E6" Then
                    CheckBoxE_1_6.Items(5).Selected = True
                End If
                If dt.Rows(0)("W1").ToString = "W1" Then
                    CheckBoxW_1_6.Items(0).Selected = True
                End If
                If dt.Rows(0)("W2").ToString = "W2" Then
                    CheckBoxW_1_6.Items(1).Selected = True
                End If

                If dt.Rows(0)("W3").ToString = "W3" Then
                    CheckBoxW_1_6.Items(2).Selected = True
                End If
                If dt.Rows(0)("W4").ToString = "W4" Then
                    CheckBoxW_1_6.Items(3).Selected = True
                End If
                If dt.Rows(0)("W5").ToString = "W5" Then
                    CheckBoxW_1_6.Items(4).Selected = True
                End If
                If dt.Rows(0)("W6").ToString = "W6" Then
                    CheckBoxW_1_6.Items(5).Selected = True
                End If
                rdoTrain_Vihicle.SelectedValue = dt.Rows(0)("TRAIN_REQ")
                If rdoTrain_Vihicle.SelectedValue = "1" Then
                    chkT.Visible = True
                End If
                chkT.DataBind()
                If dt.Rows(0)("T1").ToString = "1" Then
                    chkT.Items(0).Selected = True
                End If
                If dt.Rows(0)("T2").ToString = "2" Then
                    chkT.Items(1).Selected = True
                End If
                If dt.Rows(0)("T3").ToString = "3" Then
                    chkT.Items(2).Selected = True
                End If
                If dt.Rows(0)("T4").ToString = "4" Then
                    chkT.Items(3).Selected = True
                End If
                If dt.Rows(0)("T5").ToString = "5" Then
                    chkT.Items(4).Selected = True
                End If
                If dt.Rows(0)("T6").ToString = "6" Then
                    chkT.Items(5).Selected = True
                End If
                If dt.Rows(0)("T7").ToString = "7" Then
                    chkT.Items(6).Selected = True
                End If
                If dt.Rows(0)("T8").ToString = "8" Then
                    chkT.Items(7).Selected = True
                End If
                If dt.Rows(0)("T9").ToString = "9" Then
                    chkT.Items(8).Selected = True
                End If
                rdoTrain_Vihicle2.SelectedValue = dt.Rows(0)("VIHECLES_REQ")
                If rdoTrain_Vihicle2.SelectedValue = "2" Then
                    chkV.Visible = True
                End If
                chkV.DataBind()
                If dt.Rows(0)("V1").ToString = "1" Then
                    chkV.Items(0).Selected = True
                End If
                If dt.Rows(0)("V2").ToString = "2" Then
                    chkV.Items(1).Selected = True
                End If
                If dt.Rows(0)("V3").ToString = "3" Then
                    chkV.Items(2).Selected = True
                End If
                If dt.Rows(0)("V4").ToString = "4" Then
                    chkV.Items(3).Selected = True
                End If
                If dt.Rows(0)("V5").ToString = "5" Then
                    chkV.Items(4).Selected = True
                End If
                If dt.Rows(0)("V6").ToString = "6" Then
                    chkV.Items(5).Selected = True
                End If
                If dt.Rows(0)("V7").ToString = "7" Then
                    chkV.Items(6).Selected = True
                End If

                TxtDes_work.Text = dt.Rows(0)("DESC_OD_WORK")

            End If
        End If
    End Sub


    Sub loadDataLogin()
        Dim sql As String = ""
        sql = " select * from [user_table] where id_user =" & Session("ID_USER") & ""
        Dim dt As New Data.DataTable
        dt = clsCommand.SQL_exec(sql)
        If dt.Rows.Count > 0 Then
            TxtReqBy.Text = dt.Rows(0)("firstname").ToString & " " & dt.Rows(0)("lastname").ToString
            TxtCompReqBy.Text = dt.Rows(0)("Company").ToString & "/" & dt.Rows(0)("section").ToString
            TxtTelReqBy.Text = dt.Rows(0)("tel").ToString
            lblIDPICopTrack.Text = dt.Rows(0)("ID_USER")
        End If
    End Sub

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        search()
    End Sub

    Sub search()
        Try
            Dim sql As String = ""
            If Not IsNothing(Request.QueryString("case")) Then
                sql = " select * from [user_table] where id_picop='" & TxtPicopNo.Text & "' and [section]='" & Session("section") & "'"
            Else
                sql = " select * from [user_table] where id_picop='" & TxtPicopNo.Text & "'"
            End If

            Dim dt As New Data.DataTable
            dt = clsCommand.SQL_exec(sql)
            If dt.Rows.Count > 0 Then
                txtNamePICOP.Text = dt.Rows(0)("firstname").ToString & " " & dt.Rows(0)("lastname").ToString
                TxtCompPicop.Text = dt.Rows(0)("Company").ToString & "/" & dt.Rows(0)("section").ToString
                TxtTelPicop.Text = dt.Rows(0)("tel").ToString
                lblIDPICop.Text = dt.Rows(0)("ID_USER")
                ' TxtWorkApp.Text = txtNamePICOP.Text
                'TxtComWorkapp.Text = TxtCompPicop.Text
                'TxtTelWorkapp.Text = TxtTelPicop.Text
                ' Else
                ' img.Visible = True
                ' lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I003", TxtPicopNo.Text)
            End If
        Catch ex As Exception

        End Try
    End Sub
    Protected Sub RadioButtonTrain_Vihicle_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rdoTrain_Vihicle.SelectedIndexChanged
        If rdoTrain_Vihicle.SelectedIndex = 0 Then
            chkT.Visible = False
            chkT.ClearSelection()
        Else
            chkT.Visible = True
        End If
    End Sub
    Protected Sub rdoTrain_Vihicle2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rdoTrain_Vihicle2.SelectedIndexChanged
        If rdoTrain_Vihicle2.SelectedIndex = 0 Then
            chkV.Visible = False
            chkV.ClearSelection()
        Else
            chkV.Visible = True
        End If
    End Sub

    Protected Sub rdoWorkLocate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rdoWorkLocate.SelectedIndexChanged
        checkWorkLocation()
    End Sub

    Sub checkWorkLocation()
        Dim sql As String = ""
        Dim sql2 As String = ""
        Dim dt As New Data.DataTable
        Dim dt2 As New Data.DataTable
        Dim dfield As String = ""
        '1=station , 2= signal
        If rdoWorkLocate.SelectedValue = "1" Then
            sql = " select id,[station]+' '+ [area] as stArea from [station] "
            dt = clsCommand.SQL_exec(sql)
            dfield = "stArea"
        ElseIf rdoWorkLocate.SelectedValue = "2" Then
            If chkWest.Checked And chkEast.Checked Then
                DDlWorkLocate_FromS.Enabled = True
                DDlWorkLocate_ToS.Enabled = True
                sql2 = " select id, signal from signal where Bound='East' "
                sql = " select id, signal from signal where Bound='West' "
                dt2 = clsCommand.SQL_exec(sql2)
            ElseIf chkWest.Checked Then
                sql = " select id, signal from signal where Bound='West' "
                DDlWorkLocate_FromS.Enabled = False
                DDlWorkLocate_ToS.Enabled = False
            ElseIf chkEast.Checked Then
                sql = " select id, signal from signal where Bound='East' "
                DDlWorkLocate_FromS.Enabled = False
                DDlWorkLocate_ToS.Enabled = False
            End If

            dt = clsCommand.SQL_exec(sql)
            dfield = "signal"
        End If

        If chkEast.Checked And chkWest.Checked Then
            If rdoWorkLocate.SelectedValue = "2" Then
                DDlWorkLocate_FromS.DataSource = dt2
                DDlWorkLocate_FromS.DataTextField = dfield
                DDlWorkLocate_FromS.DataValueField = "id"
                DDlWorkLocate_FromS.DataBind()

                DDlWorkLocate_ToS.DataSource = dt2
                DDlWorkLocate_ToS.DataTextField = dfield
                DDlWorkLocate_ToS.DataValueField = "id"
                DDlWorkLocate_ToS.DataBind()
            End If
        End If
        DDlWorkLocate_From.DataSource = dt
        DDlWorkLocate_From.DataTextField = dfield
        DDlWorkLocate_From.DataValueField = "id"
        DDlWorkLocate_From.DataBind()

        DDlWorkLocate_To.DataSource = dt
        DDlWorkLocate_To.DataTextField = dfield
        DDlWorkLocate_To.DataValueField = "id"
        DDlWorkLocate_To.DataBind()
    End Sub


    Protected Sub RadioButtonEnergize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles rdoEnergize.SelectedIndexChanged
        If rdoEnergize.SelectedIndex = 0 Then
            CheckBoxE_1_6.Visible = False
            CheckBoxW_1_6.Visible = False
            CheckBoxE_1_6.ClearSelection()
            CheckBoxW_1_6.ClearSelection()
        Else
            CheckBoxE_1_6.Visible = True
            CheckBoxW_1_6.Visible = True

            'For i As Integer = 0 To RadioButtonEnergize.Items.Count

            ' Next
        End If
    End Sub

    Function checkBeforeSave() As Boolean
        Dim err As Boolean = True
        Dim msg As String = " Please check input data in block  "
        If TxtPicopNo.Text = "" Or txtNamePICOP.Text = "" Then
            msg += " PICOP NO. "
            err = False
        End If
        If TxtWorkApp.Text = "" Then
            msg += " Work Approved by"
            err = False
        End If
        If TxtComWorkapp.Text = "" Then
            msg += " Company/Section"
            err = False
        End If
        If TxtTelWorkapp.Text = "" Then
            msg += " Mobile Phone"
            err = False
        End If
        If txtDateStart.Text = "" Or txtDateEnd.Text = "" Then
            msg += " Duration of work"
            err = False
        End If

        If chkWest.Checked = False And chkEast.Checked = False Then
            msg += " Work location"
            err = False
        ElseIf rdoWorkLocate.SelectedValue = Nothing Then
            msg += " Work location Specify"
            err = False

        End If

        If DDlWorkLocate_From.SelectedValue = "0" Then
            msg += " WorkLocate_From "
            err = False
        End If

        If DDlWorkLocate_To.SelectedValue = "0" Then
            msg += " WorkLocate_To"
            err = False
        End If

        If rdoEnergize.SelectedValue = Nothing Then
            msg += " Power location"
            err = False
        End If
        If rdoTrain_Vihicle.SelectedValue = Nothing Then
            msg += " Train to be use or requested"
            err = False
        End If
        If rdoTrain_Vihicle2.SelectedValue = Nothing Then
            msg += " Vihecles to be use or requested"
            err = False
        End If

        If err = False Then
            img.Visible = True
            lblerror.Visible = True
            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "W001", msg)
        End If
        Return err
    End Function

    Protected Sub Button_Submit_Click(sender As Object, e As EventArgs) Handles Btn_Submit.Click
        Try
            Dim dates As New DateTime(2013, 2, 19, 15, 1, 0)
            Dim chkLimitDate As DateTime = New DateTime(2013, 2, 19, 15, 1, 0)

            If chkLimitDate.DayOfWeek = DayOfWeek.Monday And chkLimitDate.Hour >= 0 Or chkLimitDate.DayOfWeek = DayOfWeek.Tuesday And chkLimitDate.Hour <= 15 Then

            End If

            If checkBeforeSave() Then
                If lblidREQ.Text = "" Then
                    'insert
                    Dim sqlin As String = ""
                    sqlin += " insert into [request_table] "
                    sqlin += " (REQ_ID_PICOP,WORK_ID_PICOP,WORK_APPROVED_BY"
                    sqlin += " ,COMPANY_SECTION,MOBILE_PHONE,DUR_WORK_FORM "
                    sqlin += " ,DUR_WORK_TO,WORK_LOCA_W,WORK_LOCA_E "
                    sqlin += " ,WORK_LOCA_SPECIFY,WORK_LOCA_FROM,WORK_LOCA_TO "
                    sqlin += " ,WORK_LOCA_FROM_S,WORK_LOCA_TO_S,WORK_DESC_FROM "
                    sqlin += " ,WORK_DESC_TO,POWER_REQ,E1"
                    sqlin += " ,E2,E3,E4,E5,E6"
                    sqlin += " ,W1,W2,W3,W4,W5,W6"
                    sqlin += " ,TRAIN_REQ,T1,T2,T3,T4,T5,T6,T7,T8,T9"
                    sqlin += " ,VIHECLES_REQ,V1,V2,V3,V4,V5,V6,V7 "
                    sqlin += " ,DESC_OD_WORK,DATE_APP,STATUS_APP,DATE_REQ) values "
                    sqlin += " (" & lblIDPICopTrack.Text & "," & lblIDPICop.Text & ",'" & TxtWorkApp.Text & "'"
                    sqlin += " ,'" & TxtComWorkapp.Text & "','" & TxtTelWorkapp.Text & "','" & txtDateStart.Text & "'"
                    sqlin += " ,'" & txtDateEnd.Text & "','" & IIf(chkWest.Checked, "1", "0") & "','" & IIf(chkEast.Checked, "1", "0") & "'"
                    sqlin += " ,'" & rdoWorkLocate.SelectedValue & "'," & DDlWorkLocate_From.SelectedValue & "," & DDlWorkLocate_To.SelectedValue & ""
                    sqlin += " ," & DDlWorkLocate_FromS.SelectedValue & "," & DDlWorkLocate_ToS.SelectedValue & ",'" & txtDescript_area_from.Text & "'"
                    sqlin += " ,'" & txtDescript_area_to.Text & "','" & rdoEnergize.SelectedValue & "','" & IIf(CheckBoxE_1_6.Items(1).Selected, CheckBoxE_1_6.Items(1).Value, "") & "'"
                    sqlin += " ,'" & IIf(CheckBoxE_1_6.Items(2).Selected, CheckBoxE_1_6.Items(2).Value, "") & "','" & IIf(CheckBoxE_1_6.Items(3).Selected, CheckBoxE_1_6.Items(3).Value, "") & "','" & IIf(CheckBoxE_1_6.Items(4).Selected, CheckBoxE_1_6.Items(4).Value, "") & "'"
                    sqlin += " ,'" & IIf(CheckBoxE_1_6.Items(5).Selected, CheckBoxE_1_6.Items(5).Value, "") & "','" & IIf(CheckBoxE_1_6.Items(6).Selected, CheckBoxE_1_6.Items(6).Value, "") & "','" & IIf(CheckBoxW_1_6.Items(1).Selected, CheckBoxW_1_6.Items(1).Value, "") & "'"
                    sqlin += " ,'" & IIf(CheckBoxW_1_6.Items(2).Selected, CheckBoxW_1_6.Items(2).Value, "") & "','" & IIf(CheckBoxW_1_6.Items(3).Selected, CheckBoxW_1_6.Items(3).Value, "") & "','" & IIf(CheckBoxW_1_6.Items(4).Selected, CheckBoxW_1_6.Items(4).Value, "") & "'"
                    sqlin += " ,'" & IIf(CheckBoxW_1_6.Items(5).Selected, CheckBoxW_1_6.Items(5).Value, "") & "','" & IIf(CheckBoxW_1_6.Items(6).Selected, CheckBoxW_1_6.Items(6).Value, "") & "','" & rdoTrain_Vihicle.SelectedValue & "'"
                    If (chkT.Items.Count > 0) Then
                        sqlin += " ,'" & IIf(chkT.Items(0).Selected, chkT.Items(0).Value, "") & "','" & IIf(chkT.Items(1).Selected, chkT.Items(1).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkT.Items(2).Selected, chkT.Items(2).Value, "") & "','" & IIf(chkT.Items(3).Selected, chkT.Items(3).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkT.Items(4).Selected, chkT.Items(4).Value, "") & "','" & IIf(chkT.Items(5).Selected, chkT.Items(5).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkT.Items(6).Selected, chkT.Items(6).Value, "") & "','" & IIf(chkT.Items(7).Selected, chkT.Items(7).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkT.Items(8).Selected, chkT.Items(8).Value, "") & "'"
                    Else
                        sqlin += " ,'','','','','','','','','' "

                    End If
                    sqlin += ",'" & rdoTrain_Vihicle2.SelectedValue & "'"
                    If (chkV.Items.Count > 0) Then
                        sqlin += " ,'" & IIf(chkV.Items(0).Selected, chkV.Items(0).Value, "") & "','" & IIf(chkV.Items(1).Selected, chkV.Items(1).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkV.Items(2).Selected, chkV.Items(2).Value, "") & "','" & IIf(chkV.Items(3).Selected, chkV.Items(3).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkV.Items(4).Selected, chkV.Items(4).Value, "") & "','" & IIf(chkV.Items(5).Selected, chkV.Items(5).Value, "") & "'"
                        sqlin += " ,'" & IIf(chkV.Items(6).Selected, chkV.Items(6).Value, "") & "'"
                    Else
                        sqlin += " ,'','','','','','','' "

                    End If

                    Dim yy As String = ""
                    If Date.Now.Year > 2500 Then
                        yy = Date.Now.Date.AddYears(-543)
                    Else
                        yy = Date.Now.Date
                    End If
                    sqlin += " ,'" & TxtDes_work.Text & "','','0','" & yy & "')"
                    Dim err As String = ""
                    Try
                        err = clsCommand.SQL_cmd(sqlin)
                        If err = "" Then
                            Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditTrackRequested.aspx")
                        Else
                            img.Visible = True
                            lblerror.Visible = True
                            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                        End If
                    Catch ex As Exception
                        lblerror.Visible = True
                        lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", ex.Message.ToString)
                    End Try


                Else
                    'update
                    Dim sqlu As String = ""
                    sqlu += " UPDATE [request_table] SET "
                    sqlu += " REQ_ID_PICOP=" & lblIDPICopTrack.Text & ",WORK_ID_PICOP=" & lblIDPICop.Text & ",WORK_APPROVED_BY='" & TxtWorkApp.Text & "'"
                    sqlu += " ,COMPANY_SECTION='" & TxtComWorkapp.Text & "',MOBILE_PHONE='" & TxtTelWorkapp.Text & "',DUR_WORK_FORM='" & txtDateStart.Text & "' "
                    sqlu += " ,DUR_WORK_TO='" & txtDateEnd.Text & "',WORK_LOCA_W='" & IIf(chkWest.Checked, "1", "0") & "',WORK_LOCA_E='" & IIf(chkEast.Checked, "1", "0") & "' "
                    sqlu += " ,WORK_LOCA_SPECIFY='" & rdoWorkLocate.SelectedValue & "',WORK_LOCA_FROM='" & DDlWorkLocate_From.SelectedValue & "',WORK_LOCA_TO=" & DDlWorkLocate_To.SelectedValue & " "
                    sqlu += " ,WORK_LOCA_FROM_S=" & DDlWorkLocate_FromS.SelectedValue & ",WORK_LOCA_TO_S=" & DDlWorkLocate_ToS.SelectedValue & ",WORK_DESC_FROM='" & txtDescript_area_from.Text & "' "
                    sqlu += " ,WORK_DESC_TO='" & txtDescript_area_to.Text & "',POWER_REQ='" & rdoEnergize.SelectedValue & "',E1='" & CheckBoxE_1_6.Items(1).Value & "'"
                    sqlu += " ,E2='" & CheckBoxE_1_6.Items(2).Value & "',E3='" & CheckBoxE_1_6.Items(3).Value & "',E4='" & CheckBoxE_1_6.Items(4).Value & "',E5='" & CheckBoxE_1_6.Items(5).Value & "',E6='" & CheckBoxE_1_6.Items(6).Value & "'"
                    sqlu += " ,W1='" & CheckBoxW_1_6.Items(1).Value & "',W2='" & CheckBoxW_1_6.Items(2).Value & "',W3='" & CheckBoxW_1_6.Items(3).Value & "',W4='" & CheckBoxW_1_6.Items(4).Value & "',W5='" & CheckBoxW_1_6.Items(5).Value & "',W6='" & CheckBoxW_1_6.Items(6).Value & "'"
                    sqlu += " ,TRAIN_REQ='" & rdoTrain_Vihicle.SelectedValue & "'"
                    If (chkT.Items.Count > 0) Then
                        sqlu += ",T1='" & IIf(chkT.Items(0).Selected, chkT.Items(0).Value, "") & "',T2='" & IIf(chkT.Items(1).Selected, chkT.Items(1).Value, "") & "' "
                        sqlu += " ,T3='" & IIf(chkT.Items(2).Selected, chkT.Items(2).Value, "") & "',T4='" & IIf(chkT.Items(3).Selected, chkT.Items(3).Value, "") & "',T5='" & IIf(chkT.Items(4).Selected, chkT.Items(4).Value, "") & "'"
                        sqlu += " ,T6='" & IIf(chkT.Items(5).Selected, chkT.Items(5).Value, "") & "',T7='" & IIf(chkT.Items(6).Selected, chkT.Items(6).Value, "") & "',T8='" & IIf(chkT.Items(7).Selected, chkT.Items(7).Value, "") & "'"
                        sqlu += " ,T9='" & IIf(chkT.Items(8).Selected, chkT.Items(8).Value, "") & "'"
                    End If
                    sqlu += " ,VIHECLES_REQ='" & rdoTrain_Vihicle2.SelectedValue & "'"
                    If (chkV.Items.Count > 0) Then
                        sqlu += " ,V1='" & IIf(chkV.Items(0).Selected, chkV.Items(0).Value, "") & "'"
                        sqlu += " ,V2='" & IIf(chkV.Items(1).Selected, chkV.Items(1).Value, "") & "',V3='" & IIf(chkV.Items(2).Selected, chkV.Items(2).Value, "") & "',V4='" & IIf(chkV.Items(3).Selected, chkV.Items(3).Value, "") & "'"
                        sqlu += " ,V5='" & IIf(chkV.Items(4).Selected, chkV.Items(4).Value, "") & "',V6='" & IIf(chkV.Items(5).Selected, chkV.Items(5).Value, "") & "',V7='" & IIf(chkV.Items(6).Selected, chkV.Items(6).Value, "") & "' "
                    End If
                    sqlu += " ,DESC_OD_WORK='" & TxtDes_work.Text & "',DATE_APP='',STATUS_APP='0' "
                    sqlu += " where ID_REQUEST_FORM=" & lblidREQ.Text

                    Dim err As String = ""
                    Try
                        err = clsCommand.SQL_cmd(sqlu)
                        If err = "" Then
                            Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditTrackRequested.aspx")
                        Else
                            img.Visible = True
                            lblerror.Visible = True
                            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                        End If
                    Catch ex As Exception
                        lblerror.Visible = True
                        lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", ex.Message.ToString)
                    End Try


                End If
            End If

        Catch ex As Exception
            lblerror.Visible = True
            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", ex.Message.ToString)

        End Try


    End Sub

    Protected Sub CheckBoxE_1_6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckBoxE_1_6.SelectedIndexChanged
        If CheckBoxE_1_6.SelectedIndex = "0" Then
            If CheckBoxE_1_6.Items(0).Selected Then
                For i As Integer = 0 To CheckBoxE_1_6.Items.Count - 1
                    If i <> 0 Then
                        CheckBoxE_1_6.Items(i).Selected = True
                    End If

                Next
            Else
                CheckBoxE_1_6.ClearSelection()
            End If
        End If
    End Sub

    Protected Sub CheckBoxW_1_6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckBoxW_1_6.SelectedIndexChanged
        If CheckBoxW_1_6.SelectedIndex = "0" Then
            If CheckBoxW_1_6.Items(0).Selected Then
                For i As Integer = 0 To CheckBoxE_1_6.Items.Count - 1
                    If i <> 0 Then
                        CheckBoxW_1_6.Items(i).Selected = True
                    End If

                Next
            Else
                CheckBoxW_1_6.ClearSelection()
            End If
        End If
    End Sub

    Protected Sub chkWest_CheckedChanged(sender As Object, e As EventArgs) Handles chkWest.CheckedChanged
        checkWorkLocation()
    End Sub

    Protected Sub chkEast_CheckedChanged(sender As Object, e As EventArgs) Handles chkEast.CheckedChanged
        checkWorkLocation()
    End Sub

    Protected Sub Btn_clear_Click(sender As Object, e As EventArgs) Handles Btn_clear.Click
        clearChk()
        clearSc()
    End Sub

    Protected Sub btnAP_Click(sender As Object, e As EventArgs) Handles btnAP.Click
        Try
            If Request.QueryString("case") = "view" Then ' Approved
                Dim sqlu As String = ""
                sqlu += " UPDATE [request_table] SET "
                sqlu += " STATUS_APP='1',TP_NO='" & lblTPNO.Text & "'"
                sqlu += " where ID_REQUEST_FORM=" & lblidREQ.Text

                Dim err As String = ""
                Try
                    err = clsCommand.SQL_cmd(sqlu)
                    If err = "" Then
                        Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "TrackPossession.aspx")
                    Else
                        img.Visible = True
                        lblerror.Visible = True
                        lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                    End If
                Catch ex As Exception
                    lblerror.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", ex.Message.ToString)
                End Try

            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub DDlWorkLocate_FromS_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DDlWorkLocate_FromS.SelectedIndexChanged

    End Sub

    Protected Sub txtDateStart_TextChanged(sender As Object, e As EventArgs) Handles txtDateStart.TextChanged

    End Sub
End Class
